package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.half.model.HalfInveAdjModel;
import com.zhongwang.mms.module.half.service.HalfInveAdjService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.zhongwang.mms.common.JsonResult.fail;

/**
 * 成品库库存调整审核
 */
@Controller
@RequestMapping("/half/halInvManage/halfInveAdjAppr")
public class HalfInveAdjApprController extends BaseController {
    @Autowired
    private HalfInveAdjService halfInveAdjService;

    /**
     * 调整审核页面
     *
     * @return 审核页面
     */
    @GetMapping
    @RequiresPermissions({"halinv:haladjapp:view"})
    public String halfInveAdjApprPage(Model model) {
        model.addAttribute("whiIds", halfInveAdjService.findWhiId());
        return "half/halInvManage/halfInveAdjAppr/halfInvAdjAppr";
    }

    /**
     * 审核列表
     * 查找库存调整和盘点调整审核列表 状态为 2，3，4
     *
     * @param hAdjustmentApplyDetail 审核列表查询条件
     * @return 库存调整和库存盘点审核list
     */
    @ResponseBody
    @PostMapping("/apprList")
    @RequiresPermissions({"halinv:haladjapp:view"})
    public JsonResult HalfInveAdjApprlist(HalfInveAdjModel hAdjustmentApplyDetail) {
        startPage();
        List<HalfInveAdjModel> list = halfInveAdjService.findAdjApprList(hAdjustmentApplyDetail);
        return jsonResult(list);
    }

    /**
     * 批量审核通过库存调整
     * 将字符创转化成两个
     *
     * @param arr  所有id 字符串
     * @param arr1 所有调整类型字符串
     * @return 审核通过是否成功信息
     */
    @ResponseBody
    @PostMapping("/submit")
    @RequiresPermissions({"halinv:haladjapp:edit"})
    public JsonResult submitAjust(@Param("String arr") String arr, @Param("String arr1") String arr1) {
        List<String> list = Arrays.asList(arr.split(",")); //将id字符串转化成列表
        List<String> list1 = Arrays.asList(arr1.split(",")); //将调整类型字符串转化成列表
        List<HalfInveAdjModel> halfInveAdjModel = new ArrayList<>();
        for (int i = 0; i < list.size(); i++)//将id和调整类型列表转化成1个库存调整信息列表
        {
            HalfInveAdjModel halfInveAdjModel1 = new HalfInveAdjModel();
            halfInveAdjModel1.setAadId(Integer.valueOf(list.get(i)));
            halfInveAdjModel1.setAdjStatus(Integer.valueOf(list1.get(i)));
            halfInveAdjModel.add(halfInveAdjModel1);
        }
        int row = halfInveAdjService.submitAjustAppr(halfInveAdjModel);
        //判断审核通过成功条数和前端返回的需要审核通过的条数比较，返回审核通过成功条数消息
        int diff = list.size() - row;
        if (row < list.size()) {
            return fail("审核通过成功" + row + "条，未审核成功" + diff + "条,请检查库存数量是否小于要调整的数量。");
        } else return success("审核通过成功" + list.size() + "条!");
    }

    /**
     * 查看库存调整
     * 审核页面打开模态框，查看库存调整信息
     *
     * @param id 库存调整id
     * @return 库存调整信息
     */
    @ResponseBody
    @PostMapping("/viewAdj")
    @RequiresPermissions({"halinv:haladjapp:view"})
    public HalfInveAdjModel findInvViewAdj(Integer id) {
        return halfInveAdjService.viewInvAdjById(id);
    }


    /**
     * 查看库存盘点
     * 审核页面查看盘点信息，打卡模态框
     *
     * @param id 前端返回盘点id
     * @return 盘点信息
     */
    @ResponseBody
    @PostMapping("/viewCount")
    @RequiresPermissions({"halinv:haladjapp:view"})
    public HalfInveAdjModel findInvViewCount(Integer id) {
        return halfInveAdjService.findInvCountById(id);
    }


    /**
     * 单条审核通过库存调整
     *
     * @param id 前端返回调整id
     * @return 审核通过是否成功消息
     */
    @ResponseBody
    @PostMapping("/adjAgree")
    @RequiresPermissions({"halinv:haladjapp:edit"})
    public JsonResult halInvAdjAgree(@Param("Integer id") Integer id, @Param("Integer adjStatus") Integer adjStatus) {
        int row = halfInveAdjService.halInvAdjAgree(id, adjStatus);
        if (row < 1) {
            return fail("审核通过成功" + row + "条,请检查库存数量是否小于要调整的数量！");
        } else {
            return success("审核通过成功" + row + "条!");
        }
    }


    /**
     * 单条审核不通过库存调整和库存盘点
     *
     * @param id        前端返回调整id
     * @param adjStatus 前端返回调整类型
     * @return 审核不通过是否成功消息
     */
    @ResponseBody
    @PostMapping("/adjDisagree")
    @RequiresPermissions({"halinv:haladjapp:edit"})
    public JsonResult halInvAdjDisagree(@Param("Integer id") Integer id, @Param("Integer adjStatus") Integer adjStatus) {
        int row = halfInveAdjService.halInvAdjDisagree(id, adjStatus);
        if (row < 1) {
            return success("审核未通过成功" + row + "条,未审核成功的调整是否库存不足，不能调整？");
        } else return success("审核未通过成功" + row + "条!");
    }
}


